# coding:utf-8
from werkzeug.routing import BaseConverter
from flask import session, g, jsonify
from ihome.utils.response_code import RET
import functools

class ReConverter(BaseConverter):
    def __init__(self, url_map, regex):
        super(ReConverter, self).__init__(url_map)
        self.regex = regex


# 登录装饰器
def require_login(func):
    # 不改变函数原有属性
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        if user_id is not None:
            g.user_id = user_id
            return func(*args, **kwargs)
        else:
            return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    return wrapper